import { ref } from 'vue';
<template>
  <div class="modal">
    <el-dialog
      v-model="dialogVisible"
      :title="isNewRef ? modalConfig.header.newTitle : modalConfig.header.editTitle"
      width="30%"
      center
    >
      <div class="form">
        <el-form :model="modalFrom" label-width="120px">
          <template v-for="item in modalConfig.formItems" :key="item.prop">
            <el-form-item v-bind="item">
              <template v-if="item.type === 'input'">
                <el-input v-model="modalFrom[item.prop]" :placeholder="item.placeholder" />
              </template>
              <template v-else-if="item.type === 'date-picker'">
                <el-date-picker
                  v-model="modalFrom[item.prop]"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </template>
              <template v-else-if="item.type === 'select'">
                <el-select
                  v-model="modalFrom[item.prop]"
                  :placeholder="item.placeholder"
                  size="large"
                >
                  <template v-for="warp in item.options" :key="warp.value">
                    <el-option v-bind="warp" />
                  </template>
                </el-select>
              </template>
              <template v-else-if="item.type === 'custom'">
                <slot :name="item.slotName"></slot>
              </template>
            </el-form-item>
          </template>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmClick"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import useSystemStore from '@/store/main/system/system'
import { ref, reactive } from 'vue'

type IModalProps = {
  modalConfig: {
    pageName: string
    header: {
      newTitle: string
      editTitle: string
    }
    formItems: any[]
  }
  otherInfo?: any
}
const dialogVisible = ref(false)
const props = defineProps<IModalProps>()
const initialData: any = {}
for (const item of props.modalConfig.formItems) {
  initialData[item.prop] = item.initialValue ?? ''
}
const modalFrom = reactive(initialData)
const isNewRef = ref(true)
const editData = ref() //存储用户传过来的表单数据
function setModalVisible(isNew: boolean = true, itemData?: any) {
  isNewRef.value = isNew
  dialogVisible.value = true

  if (!isNew && itemData) {
    // 编辑数据
    for (const key in modalFrom) {
      modalFrom[key] = itemData[key]
    }
    editData.value = itemData
  } else {
    // 新建数据
    for (const key in modalFrom) {
      const item = props.modalConfig.formItems.find((item) => item.prop === key)
      modalFrom[key] = item ? item.initialValue : ''
    }

    editData.value = null
  }
}

// 点击确定的逻辑
const systemStore = useSystemStore()
function handleConfirmClick() {
  dialogVisible.value = false
  let infoData = modalFrom
  if (props.otherInfo) {
    infoData = { ...infoData, ...props.otherInfo }
  }
  if (!isNewRef.value && editData.value) {
    // 编辑数据
    console.log(systemStore, 'systemStore')

    systemStore.editPageDataAction(props.modalConfig.pageName, editData.value.id, infoData)
  } else {
    // 新建数据
    systemStore.newPageDataAction(props.modalConfig.pageName, infoData)
  }
}
defineExpose({ setModalVisible })
</script>

<style lang="less" scoped>
.form {
  padding: 0 20px;
}
</style>
